package cn.wolfcode.business.controller;

import cn.wolfcode.business.domain.BusBpmnInfo;
import cn.wolfcode.business.service.BusBpmnInfoService;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.core.page.TableDataInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/business/bpmnInfo")
public class BusBpmnInfoController extends BaseController {
    @Autowired
    private BusBpmnInfoService busBpmnInfoService;

    @GetMapping("/list")
    public TableDataInfo list( BusBpmnInfo busBpmnInfo){
        startPage();
        List<BusBpmnInfo> list = busBpmnInfoService.list(busBpmnInfo);
        return getDataTable(list);
    }

    @PostMapping
    public AjaxResult add(MultipartFile file,Integer bpmnType,String info) throws IOException {
        return toAjax(busBpmnInfoService.add(file,bpmnType,info));
    }

    @GetMapping("/{id}/readResource/{type}")
    public void readResource(@PathVariable Long id, @PathVariable String type, HttpServletResponse resp) throws IOException {

        InputStream inputStream = busBpmnInfoService.readResource(id,type);
        IOUtils.copy(inputStream,resp.getOutputStream());
    }
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        return toAjax(busBpmnInfoService.remove(id));
    }
}
